package com.apexsoft.monitor.front.console.template.service;

import com.alibaba.fastjson.JSONObject;
import com.apexsoft.front.common.support.Result;
import com.apexsoft.monitor.front.console.template.dao.TemplateDao;
import com.apexsoft.monitor.front.console.template.om.Template;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.pagination.Pagination;
import org.apache.commons.lang.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service("templateService")
public class TemplateService {
    private static final Logger log = LogManager.getRootLogger();

    @Autowired
    @Qualifier("templateDao")
    private TemplateDao templateDao = null;

    /**
     * 分页面查询数据
     *
     * @param page
     * @param templateName
     * @return
     * @throws Exception
     */
    public JSONObject listByPage(Pagination page, String templateName) throws Exception {
        JSONObject result = new JSONObject();
        EntityWrapper<Template> entityWrapper = new EntityWrapper<>();
        if (StringUtils.isNotBlank(templateName)) {
            entityWrapper.like("template_name", templateName);
        }
        List<Template> list = templateDao.selectPage(page, entityWrapper);
        if (list != null) {
            result.put(Result.CODE, Result.SC_0001);
            result.put(Result.NOTE, "查询成功");
            result.put(Result.TOTAL, page.getTotal());
            result.put(Result.ROWS, list);
        } else {
            result.put(Result.CODE, Result.EC_1000);
            result.put(Result.NOTE, "查询失败");
        }
        return result;
    }

    /**
     * 新增监控方案模板
     *
     * @param request
     * @return
     */
    public JSONObject add(HttpServletRequest request) {
        JSONObject result = new JSONObject();
        String templateName = request.getParameter("templateName");
        String templateStatus = request.getParameter("templateStatus");
        String templateType = request.getParameter("templateType");
        String templateDesc = request.getParameter("templateDesc");
        if (StringUtils.isNotBlank(templateName)
                && StringUtils.isNotBlank(templateStatus)
                && StringUtils.isNotBlank(templateType)
                ) {
            Template template = new Template();
            template.setTemplateName(templateName);
            template.setTemplateStatus(templateStatus);
            template.setTemplateType(templateType);
            template.setTemplateDesc(templateDesc);
            try {
                long insertRst = templateDao.insertTemplate(template);
                if (insertRst > Result.EC_1000) {
                    result.put(Result.CODE, Result.SC_0001);
                    result.put(Result.NOTE, "新增监控方案模板成功");
                }
            } catch (Exception e) {
                log.error("新增监控方案模板失败：" + e.getMessage(), e);
                result.put(Result.CODE, Result.EC_1000);
                result.put(Result.NOTE, "新增监控方案模板失败");
            }
        } else {
            result.put(Result.CODE, Result.EC_1000);
            result.put(Result.NOTE, "参数不正确");
        }
        return result;
    }

    /**
     * 更新监控方案模板
     *
     * @param request
     * @return
     */
    public JSONObject update(HttpServletRequest request) {
        JSONObject result = new JSONObject();
        String id = request.getParameter("id");
        String templateName = request.getParameter("templateName");
        String templateStatus = request.getParameter("templateStatus");
        String templateType = request.getParameter("templateType");
        String templateDesc = request.getParameter("templateDesc");
        if (StringUtils.isNotBlank(templateName)
                && StringUtils.isNotBlank(templateStatus)
                && StringUtils.isNotBlank(templateType)
                ) {
            Template template = new Template();
            template.setId(Integer.parseInt(id));
            template.setTemplateName(templateName);
            template.setTemplateStatus(templateStatus);
            template.setTemplateType(templateType);
            template.setTemplateDesc(templateDesc);
            try {
                int uptRst = templateDao.updateSelectiveById(template);
                if (uptRst > Result.EC_1000) {
                    result.put(Result.CODE, Result.SC_0001);
                    result.put(Result.NOTE, "修改监控方案模板机成功");
                }
            } catch (Exception e) {
                log.error("修改监控方案模板失败：" + e.getMessage(), e);
                result.put(Result.CODE, Result.EC_1000);
                result.put(Result.NOTE, "修改监控方案模板失败");
            }
        }
        return result;
    }

    /**
     * 删除监控方案模板
     *
     * @param id
     * @return
     */
    public JSONObject delete(String id) {
        JSONObject result = new JSONObject();
        if (StringUtils.isNotBlank(id)) {
            try {
                int delRst = templateDao.deleteById(id);
                if (delRst > Result.EC_1000) {
                    result.put(Result.CODE, Result.SC_0001);
                    result.put(Result.NOTE, "删除监控方案模板成功");
                }
            } catch (Exception e) {
                log.error("删除监控方案模板失败：" + e.getMessage(), e);
                result.put(Result.CODE, Result.EC_1000);
                result.put(Result.NOTE, "删除监控方案模板失败");
            }
        }else{
            result.put(Result.CODE, Result.EC_1000);
            result.put(Result.NOTE, "参数不正确");
        }
        return result;
    }

    /**
     * 查询单条记录
     *
     * @param id
     * @return
     */
    public Template getById(int id) {
        return templateDao.selectById(id);
    }

    public JSONObject getAllTemplate() {
        EntityWrapper<Template> entityWrapper = new EntityWrapper<>();
        entityWrapper.and("template_status={0}",1);
        List<Template> list = templateDao.selectList(entityWrapper);
        return Result.commonListResult(list);
    }
}
